import de.bezier.data.*;


void addParamsFromXls(Ecosystem eco, String xlsName, int sheet) {
  if (debug) println("reading params from " + xlsName);
  XlsReader reader = new XlsReader(this, xlsName);
  reader.openSheet(sheet);
  reader.firstRow();
  while (reader.hasMoreRows()) {
    reader.nextRow();
    if (debug) println("----- " + reader.getRowNum());
    if (reader.hasMoreCells()) {
      FreeParam p = addOneParamFromXls(eco, reader);
      if (debug) println(p.shortname + " (" + p.name + ") = " + p.initial);
    }
  }
}



FreeParam addOneParamFromXls(Ecosystem eco, XlsReader reader) {
  String shortname=null, units = "";
  float val=0, min=0, max=0;
  boolean logScale=false, hidden=false, highlight=false;
  String longname = reader.getString();
  if (longname.substring(0,2).equals("//")) return null; // skip over comment lines
  while (reader.hasMoreCells()) {
    reader.nextCell();
    int c = reader.getCellNum();
    switch(c) {
      case 1:
        shortname = reader.getString();
        break;
      case 2:
        units = reader.getString();
        if (units.equals("-")) units = "";
        break;
      case 3:
        val = reader.getFloat();
        break;
      case 5:
        logScale = true; // if anything is written in these, treat it as 'yes'
        break;
      case 6:
        hidden = true;
        break;
      case 7:
        highlight = true; 
        break;
      case 8:
        min = reader.getFloat();
        break;
      case 9:
        max = reader.getFloat();
        break;
      default:
        break;
    }
  }
  if (shortname == null) return null; // not a valid param if it doesn't have a shortname and a val--but a missing val is read as 0, so no easy test for it
  FreeParam p;
  if (min != 0 || max != 0) {
    p = eco.addParam(longname, shortname, units, val, min, max);
  } else {
    p = eco.addParam(longname, shortname, units, val);
  }
  p.logScale = logScale;
  p.hidden = hidden;
  p.highlight = highlight;
  return p;
}


void addCopepodFromXls(BeringEcosystem eco, String xlsName, String copepodName) {
  XlsReader reader = new XlsReader(this, xlsName);
  eco.stages = new String[0];
  String sheetName="";
  for (int sh=0; !sheetName.equals(copepodName); sh++) { // there's no way to count the available sheets, so the case where the copepodName isn't found is awkward
    reader.openSheet(sh);
    sheetName = reader.getString(0,0);
  }
  reader.firstCell();
  while (reader.hasMoreCells()) {
    reader.nextCell();
    String shortname = reader.getString();
    if (shortname.length() > 0) eco.stages = (String[])append(eco.stages,shortname);
  }
  eco.stage_longnames = new String[eco.stages.length];
  for (int i=0; i<eco.stages.length; i++) {
    eco.stage_longnames[i] = reader.getString(1, i+3);
  }
  if (debug) {
    println("stages ------");
    println(eco.stages);
    println("longnames ------");
    println(eco.stage_longnames);
  }
  reader.nextRow();
  reader.nextRow();
  reader.nextRow();
  reader.nextRow();
  while (reader.hasMoreRows()) {
    reader.nextRow();
    if (reader.hasMoreCells()) {
      addOneCopepodParamFromXls(eco, reader, eco.stages);
    }
  }
}


void addOneCopepodParamFromXls(Ecosystem eco, XlsReader reader, String[] stages) {
  String longbasename = reader.getString();
  if (longbasename.equals("") || longbasename.substring(0,2).equals("//")) return; // skip over comment lines
  reader.nextCell();
  String basename = reader.getString();
  reader.nextCell();
  String units = reader.getString();
  if (units.equals("-")) units = "";
  if (debug) {
    println("basename = " + basename);
    println("units = " + units);
  }
  for (int i=0; i<stages.length; i++) {
    reader.nextCell();
    float val = reader.getFloat();
    String shortname = strrep(basename,'*',stages[i]);
    String longname = strrep(longbasename,'*',stages[i]);
    FreeParam p = eco.addParam(longname, shortname, units, val);
    if (debug) println(p.shortname + " (" + p.name + ") = " + p.initial);
    p.hidden = true; // it wouldn't be hard to mimic the general-params sheet more closely (and the code above for reading it) and attach min,max,log,highlight,hidden to these params too
  }
}

